import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.DefaultTableXYDataset;
import org.jfree.data.xy.XYSeries;
import weerapp.meet.Meetgegevens;
import weerapp.meet.Waarneming;


@SuppressWarnings("serial")
public class GrafiekPanel extends JPanel
{
	private Meetgegevens meetgegevens;
	
	public GrafiekPanel(Meetgegevens meetgegevens)
	{
		this.meetgegevens = meetgegevens;
	}
	
	public void setPanel(int maand, int jaar)
	{
		removeAll();
		JFreeChart freechart;
		ChartPanel chartPanel;
		XYSeries maxTemp = new XYSeries("Max Temperatuur",true,false);
		XYSeries minTemp = new XYSeries("Min Temperatuur",true,false);
		
		for(int i = 1; i < 31; i++)
		{
			Waarneming waarneming = meetgegevens.getWaarneming(i, maand, jaar);
			if(waarneming != null)
			{
				maxTemp.add(i, waarneming.getMaxTemp());
				minTemp.add(i, waarneming.getMinTemp());
			}
			if(waarneming == null)
			{
				maxTemp.add(i, null);
				minTemp.add(i, null);
			}
		}
		
		DefaultTableXYDataset dataset = new DefaultTableXYDataset();
		dataset.addSeries(maxTemp);
		dataset.addSeries(minTemp);

		freechart = ChartFactory.createXYLineChart("Temperaturen", "Datum", "Temperatuur", dataset, PlotOrientation.VERTICAL, true, true, false);
		XYPlot plot = (XYPlot) freechart.getPlot();
		NumberAxis rangeAxis = (NumberAxis) plot.getDomainAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
		chartPanel = new ChartPanel(freechart);
		chartPanel.setPreferredSize(new Dimension(900, 500));
		add(chartPanel);
	}
}